//题目链接：https://www.luogu.com.cn/problem/P3385
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
const int N = 2010 , M = 6010;

int h[N] , w[M] , e[M] , ne[M] , idx;
bool st[N];
int dis[N] , cnt[N];
int n , m;

void add(int a , int b , int c)
{
    e[idx] = b , w[idx] = c , ne[idx] = h[a] , h[a] = idx++;
}
bool bellman_ford()
{
    memset(dis,0x3f,sizeof dis);
    dis[1] = 0;
    memset(cnt,0,sizeof cnt);
    queue<int> q;
    q.push(1);
    st[1] = true;
    cnt[1] = 0;
    while(!q.empty())
    {
        auto t = q.front();
        q.pop();
        st[t] = false;
        for(int i = h[t] ; i != -1 ; i = ne[i])
        {
            int j = e[i];
            if(dis[j] > dis[t] + w[i])
            {
                dis[j] = dis[t] + w[i];
                cnt[j] = cnt[t] + 1;
                if(cnt[j] >= n) return true;
                if(!st[j])
                {
                    st[j] = true;
                    q.push(j);
                }
            }
        }
    }
    return false;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(h,-1,sizeof h);
        memset(st,0,sizeof st);
        idx = 0;
        scanf("%d%d",&n,&m);
        for(int i = 0 ; i < m ; ++i)
        {
            int a , b , w;
            scanf("%d%d%d",&a,&b,&w);
            if(w >= 0)
            {
                add(a,b,w),add(b,a,w);
            }else{
                add(a,b,w);
            }
        }
        if(bellman_ford()) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
